<?php


namespace App\Model\Logic;


use App\Helper\RedisConst;
use App\Model\Entity\PostComments;
use App\Model\Entity\PostExtends;
use App\Model\Entity\PostPraise;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Redis\Pool;

/**
 * @Bean()
 * Class PostCacheLogic
 * @package App\Model\Logic
 */
class PostCacheLogic
{

    /**
     * @Inject("redis1.pool")
     * @var Pool
     */
    private $redis;


    /**
     * @Inject("redis2.pool")
     * @var Pool
     */
    private $redis2;

    //获取缓存
    public function extInfo($post_id)
    {
        $key = RedisConst::POST_EXT . $post_id;
        $ext = $this->redis->hgetAll($key);
        if($ext) return $ext;
        return $this->setExtInfo($post_id);
    }


    /**
     * --------------------------------------------------------------------------
     * 批量获取post cache
     * --------------------------------------------------------------------------
     * @param $post_ids
     * @return mixed
     */
    public function multiPostExtendCache($post_ids)
    {
        $func = function($pipe) use($post_ids){
            foreach($post_ids as $post_id){
                $key = RedisConst::POST_EXT . $post_id;
                $pipe->dump($key);
            }
        };
        return $this->redis->pipeline($func);
    }

    //重置 刷新缓存
    public function setExtInfo($post_id)
    {
        $key = RedisConst::POST_EXT . $post_id;
        $option = [ 'post_id' => $post_id, 'status' => 1];
        $praise = PostPraise::where($option)->count();
        $comment = PostComments::where($option)->count();
        $extends = PostExtends::where(['post_id' => $post_id])->first();
        if($extends) $extends = $extends->toArray();
        $read_num = $extends ? intval($extends['readNum']) : 0;
        $ext = ['read_num' => $read_num, 'praise' => $praise, 'comment' => $comment];
        $this->redis->hmset($key, $ext);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE * 60);
        return $ext;
    }

    //发布时初始化
    public function initExtInfo($post_id)
    {
        $key = RedisConst::POST_EXT . $post_id;
        $ext = ['read_num' => 0, 'praise' => 0, 'comment' => 0];
        $this->redis->hmset($key, $ext, RedisConst::NORMAL_EXPIRE * 60);
        return $ext;
    }

    /**
     * 通过cache 获取点赞的量
     */
    public function getPostPraiseTotal($post_id)
    {
        $praise_post_key = RedisConst::POSTNEWPRAISE . $post_id;

        if(!$this->redis2->exists($praise_post_key))
        {
            return 0;
        }

        return  $this->redis2->zCount($praise_post_key, 1, 905615260626830);
    }




    /**
     * --------------------------------------------------------------------------
     * 单独设置扩展字段
     * --------------------------------------------------------------------------
     * @param $post_id
     * @param $field
     * @param $value
     */
    public function hSetExtInfo($post_id, $field, $value)
    {
        $key = RedisConst::POST_EXT . $post_id;
        $exist = $this->redis->exsits($key);
        if(!$exist){
            $this->setExtInfo($post_id);
        } else{
            $this->redis->hset($key, $field, $value);
        }
    }


    //更新缓存 increment
    public function incExtData($post_id, $field, $step = 1)
    {
        $key = RedisConst::POST_EXT . $post_id;
        $exist = $this->redis->exists($key);
        if(!$exist) $this->setExtInfo($post_id);
        $this->redis->hincrby($key, $field, $step);
    }
}